from collections import OrderedDict

class OrderedDefaultDict(OrderedDict):
    def __init__(self, default_factory=None, *a, **kw):
        if (default_factory is not None and
            not callable(default_factory)):
            raise TypeError('first argument must be callable')
        OrderedDict.__init__(self, *a, **kw)
        self.default_factory = default_factory

    def __getitem__(self, key):
        try:
            return OrderedDict.__getitem__(self, key)
        except KeyError:
            return self.__missing__(key)

    def __missing__(self, key):
        if self.default_factory is None:
            raise KeyError(key)
        self[key] = value = self.default_factory()
        return value

    def __reduce__(self):
        if self.default_factory is None:
            args = tuple()
        else:
            args = self.default_factory,
        return type(self), args, None, None, self.items()

    def copy(self):
        return self.__copy__()

    def __copy__(self):
        return type(self)(self.default_factory, self)

    def __deepcopy__(self, memo):
        import copy
        return type(self)(self.default_factory,
                          copy.deepcopy(self.items()))
    def __repr__(self):
        return 'OrderedDefaultDict(%s, %s)' % (self.default_factory,
                                        OrderedDict.__repr__(self))

"""Basic Observer pattern."""

class Observable(OrderedDefaultDict):
    def __init__ (self, subscriber=None):
        OrderedDefaultDict.__init__(self, object)

        if subscriber:
            self.subscribe(subscriber)
 
    def emit (self, *args):
        '''Pass parameters to all observers and update states.'''
        for subscriber in self:
            response = subscriber(*args)
            self[subscriber] = response
 
    def subscribe (self, subscriber):
        '''Add a new subscriber to self.'''
        self[subscriber]
 
    def stat (self):
        '''Return a tuple containing the state of each observer.'''
        return tuple(self.values())

class Mode(Observable):
    pass

class Kick(Observable):
    pass

class Privmsg(Observable):
    pass

class Join(Observable):
    pass

class Part(Observable):
    pass

class Quit(Observable):
    pass

class Numeric(Observable):
    pass

class Notice(Observable):
    pass

class Nick(Observable):
    pass

class Topic(Observable):
    pass

class Privmsg_Self(Observable):
    pass

class Notice_Self(Observable):
    pass

class Disconnect(Observable):
    pass
